<template>
    <div class="manage">
        <!-- <el-button type="text" @click="dialogVisible = true">点击打开 Dialog</el-button> -->

        <el-dialog
            title="提示"
            :visible.sync="dialogVisible"
            width="50%"
            :before-close="handleClose">
            <!-- 用户表单信息 -->
            <el-form ref="form" :inline="true" :rules="rules" :model="form" label-width="80px">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
                </el-form-item>
                <el-form-item label="年龄" prop="age">
                    <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
                </el-form-item>
                <el-form-item label="性别" prop="sex">
                    <el-select v-model="form.sex" placeholder="请选择">
                        <el-option label="男" :value="1"></el-option>
                        <el-option label="女" :value="0"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="出生日期" prop="birth">
                    <el-date-picker
                        v-model="form.birth"
                        type="date"
                        placeholder="选择日期"
                        value-format="yyyy-MM-DD">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="地址" prop="addr">
                    <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
                </el-form-item>
                
            </el-form>

            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>

        <div class="manage-header">
            <el-button type="primary" @click="handleAdd">+新增</el-button>
            <!-- 搜索框 -->
            <div>
                <el-form :inline="true" :model="userForm" >
                    <el-form-item>
                        <el-input placeholder="请输入名称" v-model="userForm.name"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmitSearch">查询</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
        <div class="common-table">
            <el-table
                stripe
                height="90%"
                :data="tableData"
                style="width: 100%">
                <el-table-column
                    prop="name"
                    label="姓名">
                </el-table-column>
                <el-table-column
                    prop="sex"
                    label="性别">
                    <template slot-scope="scope">
                        <span>{{ scope.row.sex == 1 ? '男' : "女" }}</span>
                    </template> 
                </el-table-column>
                <el-table-column
                    prop="age"
                    label="年龄">
                </el-table-column>
                <el-table-column
                    prop="birth"
                    label="出生日期">
                </el-table-column>  
                <el-table-column
                    prop="addr"
                    label="地址">
                </el-table-column>
                <el-table-column
                    prop="addr"
                    label="地址">
                    <template slot-scope="scope">
                        <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDel(scope.row)">删除</el-button>
                    </template> 
                </el-table-column>
            </el-table>

            <div class="pager">
                <el-pagination
                    layout="prev, pager, next"
                    :total="total"
                    @current-change="handlePage">
                </el-pagination>
            </div>
        </div>
        
    </div>
</template>
<script>
import { addUser, editUser, getUser, delUser} from '../api'
export default {
    data() {
        return {
            dialogVisible: false,
            form: { 
                name: '',
                age: '',
                sex: '',
                birth: '',
                addr: ''
            },
            rules: {
                name: [
                    { required: true, message: '请输入姓名' }
                ],
                age: [
                    { required: true, message: '请输入年龄' }
                ],
                sex: [
                    { required: true, message: '请输入性别' }
                ],
                birth: [
                    { required: true, message: '请输入出生日期' }
                ],
                addr: [
                    { required: true, message: '请输入地址' }
                ]
            },
            tableData: [],
            modalType: 0,  // 0-新增弹框  1-编辑弹框
            total: 0,  //当前的总条数
                // 分页参数 （获取数据是传入）
            pageData: {
                page: 1,
                limit: 10

            },
            //  名称搜索
            userForm: {
                name: ''
            }
        }
    },
    methods: {
        // 处理用户提交表单
        submit(){
            this.$refs.form.validate((valid) => {
                console.log(valid, "valid-form")
                if(valid){
                    // 表单数据处理
                    console.log(this.form, 'form-data')
                    
                    if(this.modalType === 0) {
                        addUser(this.form).then(() => {
                            // 重新获取列表
                            this.getList()
                        })
                    } else {
                        editUser(this.form).then(() => {
                            // 重新获取列表
                            this.getList()
                        })
                    }

                    // 清空表单 关闭弹框
                    this.handleClose()
                }

            })
        },
        // 弹窗×掉之前执行清空表单
        handleClose(){
            this.$refs.form.resetFields()
            this.dialogVisible = false
        },
        cancel(){
            this.handleClose()
        },
        handleAdd(){  // 新增弹框
            this.modalType = 0
            this.dialogVisible = true
        },

        // 用户数据操作
        handleEdit(row){
            this.modalType = 1
            this.dialogVisible = true
            console.log(row, "edit-row")
            // this.form = row
            // 深拷贝
            this.form = JSON.parse(JSON.stringify(row))
        },
        handleDel(row){
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                delUser({id: row.id}).then(() => {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    // 重新获取列表
                    this.getList()
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });          
            });
        },
        // 获取列表数据
        getList(){  // 合并对象属性
            getUser({params: {...this.userForm, ...this.pageData}}).then( ({ data }) => {
                console.log(data)
                this.tableData = data.list
                this.total = data.count || 0
            })
        },
        // 选择页面回调函数
        handlePage(pageNum){
            console.log(pageNum, "page")
            this.pageData.page = pageNum
            this.getList()
        },
        // 列表查询
        onSubmitSearch(){
            this.getList()
        }
    },
    mounted(){
        // 获取用户列表数据
        this.getList()
    }
}
</script>
<style lang="less" scoped>
    .manage {
        height: 90%;
        .manage-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .common-table {
            position: relative;
            height: calc(100% - 22px);
            .pager {
                position: absolute;
                bottom: 0;
                right: 20px;

            }
        }
    }
</style>